package practice;

import java.util.Deque;
import java.util.LinkedList;

public class Test1 {
    public static void main(String[] args) {
        class Solution {
            public boolean isValid(String s) {
                Deque<Character> stack = new LinkedList<>();
                for(int index = 0;index < s.length();index++){
                    char c = s.charAt(index);
                    switch(c){
                        case'{':
                        case'[':
                        case'(':
                            stack.push(c);
                            break;
                        case'}':
                            if(stack.isEmpty() || stack.peek() != '{')
                                return false;
                            stack.pop();
                            break;
                        case')':
                            if(stack.isEmpty() || stack.peek() != '(')
                                return false;
                            stack.pop();
                            break;
                        case']':
                            if(stack.isEmpty() || stack.peek() !='[')
                                return false;
                            stack.pop();
                            break;
                        default:
                            return false;
                    }
                }
                return stack.isEmpty();
            }
        }
    }
}
